<?php

namespace common\models\datas;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%pta}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $site_id
 * @property integer $user_id
 * @property integer $check_time
 * @property string $check_comment
 * @property integer $apply_time
 * @property string $apply_reasons
 * @property integer $created_at
 * @property integer $states
 */
class Pta extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%pta}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['id', 'customer_id', 'site_id', 'user_id', 'check_time', 'apply_time', 'created_at', 'states'], 'integer'],
            [['check_comment', 'apply_reasons'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'customer_id' => Yii::t('common', '会员编号'),
            'site_id' => Yii::t('common', '分店编号'),
            'user_id' => Yii::t('common', '操作用户'),
            'check_time' => Yii::t('common', '审核时间'),
            'check_comment' => Yii::t('common', '审核评语'),
            'apply_time' => Yii::t('common', '申请时间'),
            'apply_reasons' => Yii::t('common', '申请理由'),
            'created_at' => Yii::t('common', '加入时间'),
            'states' => Yii::t('common', '状态'),
        ];
    }

    public function extraFields()
    {
        return [
            'customer_name' => function () {
                return !empty($this->customer) ? $this->customer->name : '';
            },
            'frontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();

            },

        ];
    }


    public function fields()
    {
        $fields = parent::fields();
        $fields['states'] = function () {
            switch ($this->states) {
                case 1:
                    return \Yii::t('frontend', '通过');
                case 0:
                    return \Yii::t('frontend', '未通过');
            }
        };
        $fields['check_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->check_time);
        };
        $fields['apply_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->apply_time);
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };

        return $fields;
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['check_time'],
                    ActiveRecord::EVENT_BEFORE_INSERT => ['apply_time'],
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],

                ],
            ],

        ]);


    }

    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }


}
